package com.seal.yoyoshop.service.impl;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Item;
import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.dao.ItemDao;
import com.seal.yoyoshop.dao.impl.ItemDaoImpl;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.ItemService;

import java.util.List;

public class ItemServiceImpl implements ItemService {

    private ItemDao itemDao = new ItemDaoImpl();
    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    public void addItemsByOrder(Order order) {
        List<Item> itemList = order.getItemList();
        for (int i = 0 ; i < itemList.size(); i ++ ){
            Item item = itemList.get(i);
            item.setOrderId(order.getId());
            boolean isSuccess = itemDao.insertItem(item);
        }
    }

    @Override
    public List<Item> getItemListByOrderId(int orderId) {
        List<Item> itemList = itemDao.SelectItemListByOrderId(orderId);
//        获取商品具体信息
        for(int i = 0 ; i < itemList.size(); i ++) {
            Item item = itemList.get(i);
            int goodsId = item.getGoodId();
            Goods goods = goodsService.getGoodById(goodsId);
            item.setGoods(goods);
        }
        return itemList;
    }
}
